package cn.edu.swpu.cins.u_chain.frontend.controller.user;

import cn.edu.swpu.cins.u_chain.frontend.entity.dto.ResultData;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.*;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.ContactInfoView;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.BasicInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.UserPublic;
import cn.edu.swpu.cins.u_chain.frontend.service.user.UserService;
import cn.edu.swpu.cins.u_chain.utils.GetCode;
import cn.edu.swpu.cins.u_chain.utils.SendSMS;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("UChain/user")
public class UserController {

    @Autowired
    private UserService service;
    @Autowired
    private GetCode getCode;
    @Autowired
    private SendSMS sendSMS;



    /**
     * 使用密码登录
     *
     * @param userLogin
     * @return
     */
    @PostMapping("loginPwd")
    public ResultData userLoginByPwd(@RequestBody UserLogin userLogin, @RequestHeader("captcha-code") String captchaCode) {
        UserSignResult result=service.loginByPwd(userLogin,captchaCode);
        return new ResultData(true, "登录成功",result.getToken());
    }


    /**
     * 用户使用验证码和验证码登录
     * @param userLogin
     * @return
     */
    @PostMapping("loginPhone")
    public ResultData userLoginByPhone(@RequestBody UserLogin userLogin) {
        UserSignResult result=service.loginByPhone(userLogin);
        return new ResultData(true, "登录成功",result.getToken());
    }

    /**
     * 登录获取图片验证码
     *
     * @param response
     * @return
     */
    @GetMapping("verifyCode")
    public ResultData getVerifyCodeForLogin(HttpServletResponse response) {
        String code = getCode.createCode(response);
        return new ResultData(true, "验证码",code);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("addUser")
    public ResultData UserRegister(@RequestBody UserPublic user) {
        service.register(user);
        return new ResultData(true, "注册成功");
    }

    /**
     * 用户忘记密码
     *
     * @return
     */
    @PostMapping("findPwd")
    public ResultData findPwd(@RequestBody ForgetPwd forgetPwd) {
        service.findPwd(forgetPwd);
        return new ResultData(true, "找回密码成功");
    }

    /**
     * 用户站内修改密码
     * @return
     */
    @PostMapping("updatePwd")
    public ResultData updatePwd(@RequestBody UpdatePwd updatePwd) {
        service.updatePwd(updatePwd);
        return new ResultData(true, "密码修改成功");
    }

    /**
     * 获取手机验证码
     * @param phoneCode
     * @return
     * @throws ClientException
     */
    @PostMapping("getPhoneVCode")
    public ResultData getPhoneVCode(@RequestBody GetPhoneCode phoneCode) throws ClientException{
        sendSMS.sendSms(phoneCode);
        return new ResultData(true, "请您在手机上查看验证码");
    }

    /**
     * 增加联系方式
     * @param contactInfoView
     * @return
     */
    @PostMapping("addContactInfo")
    public ResultData addContactInfo(@RequestBody ContactInfoView contactInfoView, @RequestHeader("userId") int userId) {
        service.addContactInfo(contactInfoView,userId);
        return new ResultData(true, "添加联系信息成功");
    }

    /**
     * 添加基本信息
     * @param basicInfo
     * @param userId
     * @return
     */
    @PostMapping("addBasicInfo")
    public ResultData addBasicInfo(@RequestBody BasicInfo basicInfo, @RequestHeader("userId") int userId) {
        service.addBasicInfo(basicInfo, userId);
        return new ResultData(true, "添加基本信息成功");
    }

    @GetMapping("getBasicInfo")
    public ResultData getBasicInfo(@RequestHeader("userId") int userId) {
        BasicInfo basicInfo = service.getBasicInfo(userId);
        return new ResultData(true, "基本信息", basicInfo);
    }

    @GetMapping("getContactInfo")
    public ResultData getContactInfo(@RequestHeader("userId") int userId) {
        ContactInfoDetail contactInfoView = service.getContactInfo(userId);
        return new ResultData(true, "联系信息", contactInfoView);
    }

    @PostMapping("updateMailForAuth")
    public ResultData confirmMail(@RequestBody UpdateBindMail updateBindMail) {
        service.checkMail(updateBindMail);
        return new ResultData(true, "请登录邮箱查看验证码");
    }


    @PostMapping("updateBindMail")
    public ResultData updateBindMail(@RequestBody UpdateBindMail updateBindMail) {
        service.updateBindMail(updateBindMail);
        return new ResultData(true, "邮箱操作成功");
    }

    @PostMapping("uploadImage")
    public ResultData uploadImage(@RequestHeader("userId") int userId,@RequestPart("image") MultipartFile image) throws IOException {
        service.uploadImage(userId, image);
        return new ResultData(true, "头像上传成功");
    }



    @PostMapping("updateBasicInfo")
    public ResultData updateBasicInfo(@RequestBody BasicInfo basicInfo) {
        service.updateBasicInfo(basicInfo);
        return new ResultData(true, "基本信息修改成功");
    }

    @PostMapping("updateContactInfo")
    public ResultData updateContactInfo(@RequestBody ContactInfoView contactInfoView) {
        service.updateContact(contactInfoView);
        return new ResultData(true, "联系方式信息修改成功");
    }

    @GetMapping("getLocation")
    public ResultData getAllLocation() {
        LocationView locationView = service.getLocation();
        return new ResultData(true, "省市县三级联动数据", locationView);
    }

    //@ExceptionHandler({ClientException.class, UserException.class,RuntimeException.class, SQLException.class})
    public ResultData handleException(Exception e) {
        return new ResultData(false,e.getMessage());
    }
}
